package cn.ncist.health_service_provider.service.impl;

import cn.ncist.health_interface.service.OrderSettingService;
import cn.ncist.health_service_provider.dao.OrderSettingMapper;
import cn.ncist.pojo.OrderSetting;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

@Transactional
@DubboService(interfaceClass = OrderSettingService.class, timeout = 50000)
public class OrderSettingServiceImpl extends ServiceImpl<OrderSettingMapper, OrderSetting> implements OrderSettingService {

    @Autowired
    private OrderSettingMapper orderSettingMapper;

    @Override
    public void add(List<OrderSetting> orderSettingList) {
        if (orderSettingList.size() > 0) {
            for (OrderSetting orderSetting : orderSettingList) {
                //判断当前日期在数据库中是否存在
                Integer count = orderSettingMapper.findCountByOrderDate(orderSetting.getOrderDate());
                if (count > 0) {
                    //存在，根据预约日期执行更新操作
//                    this.updateById(orderSetting);
                    LambdaUpdateWrapper<OrderSetting> updateWrapper = new LambdaUpdateWrapper<>();
                    updateWrapper.eq(
                            orderSetting.getOrderDate() != null, OrderSetting::getOrderDate, orderSetting.getOrderDate());
                    this.update(updateWrapper);
                } else {
                    //不存在，执行添加操作
                    this.save(orderSetting);
                }
            }
        }
    }

    @Override
    public List<Map<String,Object>> getOrderSettingByMonth(String date) {//参数格式为：2022-8
        String beginDate = date + "-1";//参数格式为：2022-8-1
        String endDate = date + "-31";//参数格式为：2022-8-31
        Map<String, String> map = new HashMap<>();
        map.put("beginDate", beginDate);
        map.put("endDate", endDate);

        List<OrderSetting> orderSettingList = orderSettingMapper.getOrderSettingByMonth(map);
        List<Map<String,Object>> data = new ArrayList<>();
        for (OrderSetting orderSetting : orderSettingList) {
            Map<String, Object> orderSettingMap = new HashMap<>();
            orderSettingMap.put("date",orderSetting.getOrderDate().getDate());
            orderSettingMap.put("number",orderSetting.getNumber());
            orderSettingMap.put("reservations",orderSetting.getReservations());
            data.add(orderSettingMap);
        }
        return data;
    }

    @Override
    public void updateByMonth(OrderSetting orderSetting) {
        if(orderSetting == null){
            throw new RuntimeException("传参错误");
        }
        Date orderDate = orderSetting.getOrderDate();
        //判断当前日期在数据库中是否存在
        Integer count = orderSettingMapper.findCountByOrderDate(orderDate);
        if (count > 0){
            //数据存在，执行更新操作
            LambdaUpdateWrapper<OrderSetting> updateWrapper = new LambdaUpdateWrapper<>();
            updateWrapper.eq(orderDate != null,OrderSetting::getOrderDate,orderDate);
            this.update(updateWrapper);
        }else {
            //数据不存在，执行添加操作
            this.save(orderSetting);
        }
    }
}
